app.service('z-init-s', [ '$rootScope', '$z-http', function($rootScope, $http) {
	var service = {
			init:function(getSuccess,fault){
				$http.post(
						"user/getInitialized.do"
						).success(function(data) {
							getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
			}
	};
	return service;
} ]);

/**
 * 全局页面导航服务
 */
app.service('z-nav-s', [ '$rootScope', '$z-http','ZModel', function($rootScope, $http,ZModel) {
	var service = {

			changeLocation:function(newValue) {
				
			},
			getMenuData:function(getSuccess,fault){
				$http.post(
						"menu/getMenuTreeByLoginUser.do"
						).success(function(data) {
							getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
			}
	};
	return service;
} ]);


app.service('z-user-s', [ '$rootScope', '$z-http','ZModel',
                   		function($rootScope, $http, ZModel) {
  	var service = {
  			checkKey:function(data,success,fault){
				$http.post(
  						"user/checkKey.do",
  						$.param(data),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
  						).success(function(data) {
  							success(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
			},
  			getLoginUser:function(getSuccess,fault){
  				$http.post("user/getUserData.do").success(function(data) {
  					getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
//				ZFrame.ajax({
//  					url : "user/getUserData.do",
//  					type : "POST",
//  					dataType:"json",
//  					success : function(data) {
//  						if(data.result == 1){
//  							getSuccess(data.success);
//  						} else {
//  							if(fault) {
//  	  							fault();
//  	  						}
//  						}
//  					}
//  				});
			},
  			getUserData : function(getSuccess,fault) {
  				$http.post("user/getUserList.do").success(function(data) {
  					getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
  			},
  			deleteUser:function(userId,delSuccess,fault){
  				$http.post(
  						"user/deleteUser.do",
  						$.param({id:userId}),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
  						).success(function(data) {
  							delSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
  			},
  			saveUser:function(user,saveSuccess,fault){
  				$http.post(
  						"user/updateUser.do",
  						$.param({
  	  						accountId:user.accountId,
  	  						name:user.name,
  	  						password:user.password,
  	  						phone:user.phone,
  	  						deptId:user.deptId,
  	  						roleId:user.roleId
  	  						}),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
  						).success(function(data) {
  							saveSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
  			},
  			addUser:function(user,addSuccess,fault){
  				$http.post(
  						"user/addUser.do",
  						$.param({
  	  						accountId:user.accountId,
  	  						name:user.name,
  	  						password:user.password,
  	  						phone:user.phone,
  	  						deptId:user.deptId,
  	  						roleId:user.roleId
  	  						}),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
  						).success(function(data) {
  							addSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
  			},
  			getUserList : function(data,success,fault) {
				$http.post(
						"user/getUserList.do",
						$.param(data),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
						).success(function(data) {
					success(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault(data);
  					}
  				});
			},
			addUser:function(data,success,fault){
				$http.post("user/addUser.do",data)
				.success(function(data) {
					success(data.success);
  				}).error(function(data) {
						fault(data);
  				});
			},
			updateUser:function(data,success,fault){
				$http.post("user/updateUser.do",data)
				.success(function(data) {
					success(data.success);
  				}).error(function(data) {
						fault(data);
  				});
			},
			getUserById:function(Id,success,fault){
				$http.post(
						"user/getUserById.do",
						$.param({userId:Id}),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
						).success(function(data) {
					success(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault(data);
  					}
  				});	
			},
			disableById:function(Id,success,fault){
				$http.post(
						"user/disableById.do",
						$.param({userId:Id}),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
						).success(function(data) {
					success(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault(data);
  					}
  				});	
			},
			startById:function(Id,success,fault){
				$http.post(
						"user/startById.do",
						$.param({userId:Id}),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
						).success(function(data) {
					success(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault(data);
  					}
  				});	
			},
			getSingleSignOn:function(data,success,fault){
				$http.post("user/getSingleSignOn.do",
						$.param(data),
						{headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}
					).success(function(data) {
						success(data.success);
					}).error(function(data) {
						fault(data);
					});
			}
  	};
  	return service;
  }
  ]);

app.service('z-edit-menu-s',['$rootScope', '$http','ZModel',
                       		function($rootScope, $http, ZModel){
	var service = {
			getMenuData:function(getSuccess,fault){
				$http.post("menu/getMenuTree.do").success(function(data) {
					getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
			},
			getMenuList:function(getSuccess,fault){
				$http.post("menu/getMenuList.do").success(function(data) {
					getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
			},
			saveMenuList:function(menuList,getSuccess,fault){
				$http.post(
						"menu/addMenuTree.do",
						menuList
						).success(function() {
							getSuccess();
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
			},
			getMenuByUrl : function(data,success,fault) {
				$http.post(
						"menu/getMenuByUrl.do",
						$.param(data),
						{headers: {
							   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
						 }}
						).success(function(data) {
					success(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault(data);
  					}
  				});
			}
	};
	return service;
}]);

app.service('z-role-s',['$rootScope', '$http','ZModel',
                        		function($rootScope, $http, ZModel){
 	var service = {
 			getRoleData:function(getSuccess,fault){
 				$http.post(
						"role/getRoleTree.do"
						).success(function(data) {
							getSuccess(data.success);
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
 			},
 			setRoleDate:function(treeData,getSuccess,fault){
 				$http.post(
						"role/addRoleTree.do",
						treeData
						).success(function() {
							getSuccess();
  				}).error(function(data) {
  					if(fault) {
  						fault();
  					}
  				});
 			}
 	};
 	return service;
 }]);

app.service('z-login-s', [ '$rootScope', '$z-http', 'ZModel',
		function($rootScope, $http, ZModel) {
			var service = {
				login:function(loginName,password,CSRFToken,loginSuccess,loginFault){
//					ZFrame.ajax({
//	 					url:"user/login.do",
//	 					type:"POST",
//	 					data:{loginname:loginName,password:password},
//	 					dataType:"json",
//	   					success : function(data) {
//	   						if(data.result == 1){
//	   							loginSuccess(data.success);
//	   						}else{
//	   							loginFault(data.fault);
//	   						}
//	   					}
//	   				});
					var headers = {};
					headers["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";
					headers["CSRFToken"]=CSRFToken;
					
					$http.post("user/login.do",
							$.param({loginname:loginName,password:password}),
							{headers:headers}).success(function(data) {
	   					loginSuccess(data.success);
	  				}).error(function(data) {
	  					if(loginFault) {
	  						loginFault(data.fault);
	  					}
	  				});
					/*$http.post("user/login.do",$.param({loginname:loginName,password:password})).success(function(data) {
						loginSuccess(data.success);
   	  				}).error(function(data) {
	   	  				if(loginFault) {
	  						loginFault(data.fault);
	  					}
   	  				});*/
				},
				getVerificationPassword:function(loginName,password,CSRFToken,loginSuccess,loginFault){
					var headers = {};
					headers["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";
					headers["CSRFToken"]=CSRFToken;
					
					$http.post("user/getVerificationPassword.do",
							$.param({loginname:loginName,password:password}),
							{headers: headers}).success(function(data) {
	   					loginSuccess(data.success);
	  				}).error(function(data) {
	  					if(loginFault) {
	  						loginFault(data.fault);
	  					}
	  				});
				},
				forgetPassword:function(CSRFToken,data,success,fault){
					var headers = {};
					headers["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8";
					headers["CSRFToken"]=CSRFToken;
					$http.post("user/forgetPassword.do",
							$.param(data),
							{headers: headers}).success(function(data) {
								success(data.success);
	  				}).error(function(data) {
	  					if(fault) {
	  						fault(data.fault);
	  					}
	  				});
				},
				getCSRFToken:function(loginSuccess,loginFault){
					$http.post("user/getCSRFToken.do").success(function(data) {
   	   					loginSuccess(data.success);
   	  				}).error(function(data) {
	  					loginFault(data.fault);
   	  				});
				}
			};
			return service;
		} ]);

app.service('z-logout-s', ['$z-http',
                   		function($http) {
                   			var service = {
                   				logout:function(loginSuccess,loginFault){
                   					$http.post("user/logout.do").success(function(data) {
                   	   					loginSuccess(data.success);
                   	  				}).error(function(data) {
               	  						loginFault(data.fault);
                   	  				});
                   				},getTips:function(success,fault){
                   					$http.post("tips/getTips.do").success(function(data) {
                   						success(data.success);
                   	  				}).error(function(data) {
                   	  					fault(data.fault);
                   	  				});
                   				},getPrivilegeGroupIdType:function(success,fault){
                   					$http.post("tips/getPrivilegeGroupIdType.do").success(function(data) {
                   						success(data.success);
                   	  				}).error(function(data) {
                   	  					fault(data.fault);
                   	  				});
                   				}
                   			};
                   			return service;
                   		} ]);

app.service('z-permission-s', [ '$rootScope', '$z-http', 'ZModel',
		function($rootScope, $http, ZModel) {
			var service = {
				getPrivilegeGroupMenuById : function(privilegeGroupId, getSuccess,fault) {
					$http.post(
							"privilege/getPrivilegeGroupMenuById.do",
							$.param({
								privilegeGroupId : privilegeGroupId
							}),
							{headers: {
								   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
							 }}
							).success(function(data) {
								getSuccess(data.success);
	  				}).error(function(data) {
	  					if(fault) {
	  						fault();
	  					}
	  				});
				},
				addPrivilegeGroupMenu : function(permissionList, setSuccess,fault) {
					$http.post(
							"privilege/addPrivilegeGroupMenu.do",
							permissionList
							).success(function() {
								setSuccess();
	  				}).error(function(data) {
	  					if(fault) {
	  						fault(data);
	  					}
	  				});
				},
				updatePrivilegeGroupMenu : function(permissionList, setSuccess,fault) {
					$http.post(
							"privilege/updatePrivilegeGroupMenu.do",
							permissionList
							).success(function() {
								setSuccess();
	  				}).error(function(data) {
	  					if(fault) {
	  						fault(data);
	  					}
	  				});
				},
				deletePrivilegeGroupMenu : function(privilegeGroupId, getSuccess,fault) {
					$http.post(
							"privilege/deletePrivilegeGroupMenu.do",
							$.param({
								privilegeGroupId : privilegeGroupId
							}),
							{headers: {
								   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
							 }}
							).success(function() {
								getSuccess();
	  				}).error(function(data) {
	  					if(fault) {
	  						fault(data);
	  					}
	  				});
				},
				getPrivilegeGroupTree : function(getSuccess,fault) {
					$http.post(
							"privilege/getPrivilegeGroupTree.do"
							).success(function(data) {
								getSuccess(data.success);
	  				}).error(function(data) {
	  					if(fault) {
	  						fault();
	  					}
	  				});
				}
			};
			return service;
		} ]);

